#include <gpdr.h>

void init_ports()
{
// Declare your local variables here

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x0B ;PORTD
#endasm       

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=Out 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=1 State0=0
PORTA=0x00;
DDRA=0x03;

// Port B initialization
// Func7=Out Func6=In Func5=Out Func4=Out Func3=Out Func2=In Func1=In Func0=In // for SPI, and AIN1 
// State7=0 State6=T State5=0 State4=0 State3=0 State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0xB8;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=Out Func3=In Func2=Out Func1=In Func0=In // Dust Sensor
// State7=T State6=T State5=T State4=1 State3=T State2=1 State1=P State0=P // Pull Up TWI Lines
PORTC=0x17;
DDRC=0x14;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // for LCD
// State7=T State6=T State5=T State4=T State3=0 State2=T State1=0 State0=T 
PORTD=0x00;
DDRD=0x00; // for LCD

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=In Func1=Out Func0=In // for UART
// State7=T State6=T State5=T State4=T State3=0 State2=T State1=0 State0=T 
//PORTD=0x00;
//DDRD=0x0A; // for UART

//// State7=T State6=T State5=T State4=T State3=P State2=P State1=P State0=P  // Pull Up TXD1, RXD1, TWI Lines
//PORTD=0x0F;
//// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
//DDRD=0x00;
}

void init_timers()
{
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Rising Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x42;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2A output: Disconnected
// OC2B output: Disconnected
ASSR=0x00;
TCCR2A=0x00;
TCCR2B=0x00;
TCNT2=0x00;
OCR2A=0x00;
OCR2B=0x00;
}

void init_interrupts()
{
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-15: Off
// Interrupt on any change on pins PCINT16-23: Off
// Interrupt on any change on pins PCINT24-31: Off
EICRA=0x00;
EIMSK=0x00;
PCICR=0x00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x00;
// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=0x00;
// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=0x00;
}

void init_usarts()
{
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
//// USART0 Baud Rate: 9600
// USART0 Baud Rate: 38400
UCSR0A=0x00;
UCSR0B=0xD8; // for INT
//UCSR0B=0x18; // for polling
UCSR0C=0x06;
UBRR0H=0x00;
//UBRR0L=0x33; // for 9600
UBRR0L=0x0C; // for 38400

// USART1 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART1 Receiver: On
// USART1 Transmitter: On
// USART1 Mode: Asynchronous
//// USART1 Baud Rate: 9600
// USART1 Baud Rate: 38400
UCSR1A=0x00;
UCSR1B=0xD8; // for INT
//UCSR1B=0x18; // for polling
UCSR1C=0x06;
UBRR1H=0x00;
//UBRR1L=0x33; // for 9600
UBRR1L=0x0C; // for 38400
}

void init_comparator()
{
// Analog Comparator initialization
// Analog Comparator: On
// Analog Comparator Input Capture by Timer/Counter 1: On
ACSR=0x44;
ADCSRB=0x40;
}

void init_ADC()
{
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AVCC pin
// Only the 8 most significant bits of
// the AD conversion result are used
// Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On
// ADC4: On, ADC5: On, ADC6: On, ADC7: On
DIDR0=0x00;
ADMUX=ADC_VREF_AVCC + 3; // ADC3 First
//ADMUX=ADC_VREF_INTL;
ADCSRA=0x8B; // Enable ADC
}

void init_TWI()
{
// 2 Wire Bus initialization
// Generate Acknowledge Pulse: Off
// 2 Wire Bus Slave Address: 0h
// General Call Recognition: Off
// Bit Rate: 160.000 kHz
TWSR=0x00;
TWBR=0x10;
TWAR=0x00;
TWCR=0x04;
}
